package com.sansan.qiangji.realize.device;

import com.sansan.qiangji.VO.device.stackerParam.StackerParamListResultVO;
import com.sansan.qiangji.VO.device.stackerParam.StackerParamVO;
import com.sansan.qiangji.entity.device.StackerParam;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.device.stackerParam.StackerParamAddForm;
import com.sansan.qiangji.form.device.stackerParam.StackerParamEditForm;
import com.sansan.qiangji.form.device.stackerParam.StackerParamListForm;
import com.sansan.qiangji.service.impl.device.StackerParamServiceImpl;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StackerParamReal {
    private StackerParamServiceImpl stackerParamService;

    @Resource
    public void setStackerParamService(StackerParamServiceImpl stackerParamService) {
        this.stackerParamService = stackerParamService;
    }

    public StackerParamListResultVO stackerParamList(StackerParamListForm stackerParamListForm) {
        PageResult<StackerParam> stackerParamPageResult = stackerParamService.stackerParamList(stackerParamListForm);
        StackerParamListResultVO stackerParamListResultVO = new StackerParamListResultVO();
        BeanUtils.copyProperties(stackerParamPageResult, stackerParamListResultVO);
        List<StackerParamVO> stackerParamVOS = new ArrayList<>();
        for (StackerParam stackerParam : stackerParamPageResult.getContent()) {
            StackerParamVO stackerParamVO = new StackerParamVO();
            BeanUtils.copyProperties(stackerParam, stackerParamVO);
            stackerParamVOS.add(stackerParamVO);
        }
        stackerParamListResultVO.setStackerParamVOS(stackerParamVOS);
        return stackerParamListResultVO;
    }

    @Transactional
    public void addStackerParam(StackerParamAddForm stackerParamAddForm) {
        List<StackerParam> stackerParams = stackerParamService.findStackerParamName(stackerParamAddForm.getStackerParamName());
        if (stackerParams.size() != 0) {
            log.error("【添加堆垛机参数】堆垛机参数已存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_IS_EXIST);
        }
        List<String> stackerParamTypeList = new ArrayList<>();
        stackerParamTypeList.add("Word");
        stackerParamTypeList.add("DWord");
        stackerParamTypeList.add("Int");
        stackerParamTypeList.add("DInt");
        stackerParamTypeList.add("IX");
        stackerParamTypeList.add("QX");
        stackerParamTypeList.add("MX");
        if (stackerParamTypeList.stream().noneMatch(e->e.equals(stackerParamAddForm.getStackerParamType()))) {
            log.error("【添加堆垛机参数】堆垛机参数类型不存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_TYPE_IS_NOT_EXIST);
        }
        StackerParam stackerParam = new StackerParam();
        BeanUtils.copyProperties(stackerParamAddForm, stackerParam);
        stackerParamService.addStackerParam(stackerParam);
    }

    @Transactional
    public void editStackerParam(StackerParamEditForm stackerParamEditForm) {
        List<String> stackerParamTypeList = new ArrayList<>();
        stackerParamTypeList.add("Word");
        stackerParamTypeList.add("DWord");
        stackerParamTypeList.add("Int");
        stackerParamTypeList.add("DInt");
        stackerParamTypeList.add("IX");
        stackerParamTypeList.add("QX");
        stackerParamTypeList.add("MX");
        if (stackerParamTypeList.stream().noneMatch(e->e.equals(stackerParamEditForm.getStackerParamType()))) {
            log.error("【编辑堆垛机参数】堆垛机参数类型不存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_TYPE_IS_NOT_EXIST);
        }
        StackerParam stackerParam = stackerParamService.findById(stackerParamEditForm.getStackerParamId());
        if (stackerParam == null) {
            log.error("【编辑堆垛机参数】堆垛机参数不存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_IS_NOT_EXIST);
        }
        if (!stackerParam.getStackerParamName().equals(stackerParamEditForm.getStackerParamName())) {
            List<StackerParam> stackerParamList = stackerParamService.findStackerParamName(stackerParamEditForm.getStackerParamName());
            if (stackerParamList.size() != 0) {
                log.error("【添加堆垛机参数】堆垛机参数已存在");
                throw new ServiceException(ResultEnum.STACKER_PARAM_IS_EXIST);
            }
        }
        BeanUtils.copyProperties(stackerParamEditForm, stackerParam);
        stackerParamService.updateStackerParam(stackerParam);
    }

    @Transactional
    public void deleteStackerParam(Integer stackerParamId) {
        StackerParam stackerParam = stackerParamService.findById(stackerParamId);
        if (stackerParam == null) {
            log.error("【删除堆垛机参数】堆垛机参数不存在");
            throw new ServiceException(ResultEnum.STACKER_PARAM_IS_NOT_EXIST);
        }
        stackerParamService.deleteById(stackerParamId);
    }
}
